package net.lzzy.practices.web.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Result;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.ResultService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.KeyInfo;
import net.lzzy.practices.web.models.VmResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(tags = "结果相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_RESULT_ROOT)
public class ResultApiController {
    private final ResultService service;

    @Autowired
    public ResultApiController (ResultService service){
        this.service=service;
    }

    @ApiOperation(value = "提交结果", notes = "<b>参数：<b/><br/>token值<br/>")
    @PostMapping(ApiConstants.ROUTE_RESULT_POST)
    public ApiResult postResult(@RequestBody JSONObject json){
        String token=json.getString(ApiConstants.KEY_TOKEN);
        if (StringUtils.isEmpty(token)){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        JSONArray array=json.getJSONArray(ApiConstants.KEY_RESULT_OPTION_ARRAY);
        if (array==null){
            return  new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        List<VmResult> results =new ArrayList<>();
        for (int i =0 ;i<array.size();i++){
            JSONObject jo=array.getJSONObject(i);
            String strQuestionId=jo.getString(ApiConstants.KEY_RESULT_OPTION_QUESTION_ID);
            JSONArray arrOptions=jo.getJSONArray(ApiConstants.KEY_RESULT_OPTION_ID);
            UUID questionId = UUID.fromString(strQuestionId);
            List<UUID> optionIds=new ArrayList<>();
            arrOptions.forEach(o -> optionIds.add(UUID.fromString(o.toString())));
            results.add(new VmResult(service.add(questionId, optionIds, token)));
        }
        return new ApiResult(true,results.size()+"",results);
    }

    @ApiOperation(value = "删除提交的结果", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>" +
            "2.strResultId(String)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_RESULT_DEL)
    public ApiResult remove(@RequestBody KeyInfo info){
        String token = info.getToken();
        String strResultId=info.getId();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strResultId)) {
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID resultId=UUID.fromString(strResultId);
        Messages msg= service.remove(resultId, token);
        return new ApiResult(msg==Messages.SUCCESS,msg.toString(),null);
    }

    @ApiOperation(value = "删除提交的结果", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>" +
            "2.strResultId(String)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_RESULT_CHAPTER_DEL)
    public ApiResult removeChapter(@RequestBody KeyInfo info){
        String token = info.getToken();
        String strChapterId=info.getId();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strChapterId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(),null);
        }
        UUID chapterId=UUID.fromString(strChapterId);
        Messages msg=service.removeChapter(chapterId,token);
        return new ApiResult(msg==Messages.SUCCESS,msg.toString(),null);
    }

    @ApiOperation(value = "获取学生章节的所有题目结果", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>" +
            "2.studentId(String)<br/>3.chapterId(String)<br/>")
    @GetMapping(ApiConstants.ROUTE_RESULT_CHAPTER)
    public ApiResult getChapterResult(@RequestParam (name = "sid")UUID studentId,
                                      @RequestParam (name = "cid")UUID chapterId,
                                      @RequestParam String token){
        if (AuthUtils.illegalUser(token,new int[]{User.TYPE_STUDENT})){
            return new ApiResult(false,Messages.NO_PERMISSION.toString(), null);
        }
        List<VmResult> results=new ArrayList<>();
        service.getChapterResults(studentId,chapterId).forEach(result -> results.add(new VmResult(result)));
        return new ApiResult(true,results.size()+"",results);
    }

    @ApiOperation(value = "教师获取学生所有题目提交的结果", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>" +
            "2.questionId(String)<br/>")
    @GetMapping(ApiConstants.ROUTE_RESULT_QUESTION)
    public ApiResult getQuestionResults(@RequestParam(name = "qid")UUID questionId,
                                        @RequestParam String token){
        List<VmResult> results= new ArrayList<>();
        List<Result> list=service.getQuestionResults(questionId, token);
        if (list==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        list.forEach(result -> results.add(new VmResult(result)));
        return new ApiResult(true,results.size()+"",results);
    }

    @ApiOperation(value = "判断学生某一章节是否提交", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>" +
            "2.studentId(String)<br/>3.chapterId(String)<br/>")
    @GetMapping(ApiConstants.ROUTE_RESULT_COMMITTED)
    public ApiResult isChapterCommitted(@RequestParam(name = "sid")UUID studentId,
                                        @RequestParam(name = "cid")UUID chapterId,
                                        @RequestParam String token){
        Boolean result=service.isChapterCommitted(studentId, chapterId, token);
        String message = result==null?Messages.NO_PERMISSION.toString() : "";
        return new ApiResult(true,message,result);
    }
}
